package com.example.l3.ktm;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.os.Environment;
import android.util.Log;

public class FolderFileManager {
	static FolderFileManager _share=null;
	static List<String> _compression = Arrays.asList("rar","zip","7z");
	
	
	public static FolderFileManager share()
	{
		if(_share==null)
			_share=new FolderFileManager();
		
		return _share;
	}
	
	public FolderFileManager()
	{}
	
	public String folderDownload()
	{
		String pathFolderDownload = Environment.getExternalStorageDirectory()+"/Download";
		File folderDownload = new File(pathFolderDownload);
		if(!folderDownload.exists())
			folderDownload.mkdir();
		
		return folderDownload.getPath();
	}
	
	public String createFile(String filename) throws IOException
	{
		File director = new File(folderDownload());
		File file = new File(director,filename);
		file.createNewFile();
		
		return file.getPath();
	}
	
	public String createFileFromURL(String url) throws IOException
	{
		String fileName = getFileNameFromURL(url);
		fileName=formatFileNameURL(fileName);
		String directory = pathForFilename(fileName);
		
		File file = new File(directory,fileName);
		file.createNewFile();
		
		return file.getPath();
	}
	
	public String getExtensionFile(String filename){
		return filename.substring(filename.lastIndexOf("."), filename.length()-1);
	}
	
	public String pathForFilename(String filename){
		String folderName = "";
		String extensionName = getExtensionFile(filename);
		
		if(_compression.contains(extensionName))
			folderName = "Compressed";
		else if ( extensionName.equals("doc") || extensionName.equals("pdf") || extensionName.equals("ppt")
				|| extensionName.equals("xls")) { // file type documents
			folderName = "Document";
		}		
		else if ( extensionName.equals("mp3") || extensionName.equals("wav") || extensionName.equals("wma")
				|| extensionName.equals("aac") || extensionName.equals("aif") 
				|| extensionName.equals("m4a")){ // file type music
			folderName = "Music";
		}
		else if ( extensionName.equals("exe") || extensionName.equals("msi") || extensionName.equals("apk")
				){ // file type program
			folderName = "Program";
		}
		else if ( extensionName.equals("avi") || extensionName.equals("mpeg") || extensionName.equals("wmv")
				|| extensionName.equals("mp4") || extensionName.equals("m4v") || extensionName.equals("ogg"))
		{ // file type video
			folderName = "Video";
		}
		else if ( extensionName.equals("jpg") || extensionName.equals("jpeg") || extensionName.equals("png")
				){ // file type image
			folderName = "Picture";
		}
		else {// file type general
			folderName = "General";
		}
		
		File directory = new File(folderDownload()+"/"+folderName);
		if(!directory.exists())
			directory.mkdir();

		return directory.getPath();
	}
	
	public String getFileNameFromURL(String url)
	{
		return url.substring(url.lastIndexOf("/")+1, url.length());
	}
	
	public String formatFileNameURL(String filenameURL)
	{
		return filenameURL.replace("+", "_");
	}
}
